package com.meilingcloud.research.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.meilingcloud.common.annotation.Log;
import com.meilingcloud.common.core.controller.BaseController;
import com.meilingcloud.common.core.domain.AjaxResult;
import com.meilingcloud.common.enums.BusinessType;
import com.meilingcloud.common.utils.SecurityUtils;
import com.meilingcloud.common.utils.poi.ExcelUtil;
import com.meilingcloud.research.domain.RwsTask;
import com.meilingcloud.research.domain.RwsTaskSample;
import com.meilingcloud.research.service.IRwsTaskService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 课题任务 Controller
 *
 * @author chenl
 * @date 2021-03-23
 */
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/research/task")
public class RwsTaskController extends BaseController {

    private final IRwsTaskService iRwsTaskService;

    /**
     * 查询课题任务 列表
     */
    @PreAuthorize("@ss.hasPermi('research:task:list')")
    @GetMapping("/list")
    public AjaxResult list(RwsTask rwsTask) {
        List<RwsTask> list = iRwsTaskService.queryList(rwsTask);
        return AjaxResult.success(list);
    }

    /**
     * 获取待完成任务数量
     */
    @GetMapping("/toTaskCount")
    public AjaxResult getToTaskCount() {
        return AjaxResult.success(iRwsTaskService.getToTaskCount());
    }

    /**
     * 根据课题id 获取任务列表
     */
    @GetMapping("/listAll/{topicId}")
    public AjaxResult listAll(@PathVariable("topicId") Long topicId) {
        List<RwsTask> list = iRwsTaskService.list(new LambdaQueryWrapper<RwsTask>().eq(RwsTask::getTopicId, topicId));
        return AjaxResult.success(list);
    }

    /**
     * 导出课题任务 列表
     */
    @PreAuthorize("@ss.hasPermi('research:task:export')")
    @Log(title = "课题任务 ", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(RwsTask rwsTask) {
        List<RwsTask> list = iRwsTaskService.queryList(rwsTask);
        ExcelUtil<RwsTask> util = new ExcelUtil<RwsTask>(RwsTask.class);
        return util.exportExcel(list, "task");
    }

    /**
     * 获取课题任务 详细信息
     */
    @PreAuthorize("@ss.hasPermi('research:task:query')")
    @GetMapping(value = "/{taskId}")
    public AjaxResult getInfo(@PathVariable("taskId") Long taskId) {
        return AjaxResult.success(iRwsTaskService.getTaskById(taskId));
    }

    /**
     * 新增课题任务
     */
    @PreAuthorize("@ss.hasPermi('research:task:add')")
    @Log(title = "课题任务 ", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody RwsTask rwsTask) {
        rwsTask.setCreateBy(SecurityUtils.getUsername());
        return toAjax(iRwsTaskService.saveTask(rwsTask));
    }

    /**
     * 修改课题任务
     */
    @PreAuthorize("@ss.hasPermi('research:task:edit')")
    @Log(title = "课题任务 ", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody RwsTask rwsTask) {
        rwsTask.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(iRwsTaskService.updateTask(rwsTask));
    }

    /**
     * 删除课题任务
     */
    @PreAuthorize("@ss.hasPermi('research:task:remove')")
    @Log(title = "课题任务 ", businessType = BusinessType.DELETE)
    @DeleteMapping("/{taskIds}")
    public AjaxResult remove(@PathVariable Long[] taskIds) {
        return toAjax(iRwsTaskService.removeByIds(Arrays.asList(taskIds)));
    }

    /**
     * 取消样本与任务的关联
     */
    @PostMapping("/cancelRelate")
    @Log(title = "课题任务 ", businessType = BusinessType.OTHER)
    public AjaxResult cancelRelate(@RequestBody RwsTaskSample taskSample) {
        return toAjax(iRwsTaskService.removeRelate(taskSample));
    }

    /**
     * 增加关联
     */
    @PostMapping("/addRelate")
    @Log(title = "课题任务 ", businessType = BusinessType.OTHER)
    public AjaxResult addRelate(@RequestBody List<RwsTaskSample> taskSamples) {
        return toAjax(iRwsTaskService.addRelate(taskSamples));
    }

}
